package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.net.Uri;
import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.downloader.Downloader;
import com.stark.downloader.FailCause;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.adapter.CollectAdapter;
import flc.ast.databinding.ActivityCollectBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;

public class CollectActivity extends BaseAc<ActivityCollectBinding> {

    private List<StkResBean> listShow = new ArrayList<>();
    private CollectAdapter collectAdapter;
    private boolean isEdit = false;
    private List<StkResBean> listSel = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.activity_collect;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivMyCollectBack.setOnClickListener(v -> onBackPressed());
        mDataBinding.ivCollectEdit.setOnClickListener(this);
        mDataBinding.tvAllSel.setOnClickListener(this);
        mDataBinding.tvCancelEdit.setOnClickListener(this);
        mDataBinding.tvCancelCollect.setOnClickListener(this);
        mDataBinding.tvDownload.setOnClickListener(this);
        mDataBinding.ivGoHome.setOnClickListener(this);

        mDataBinding.rvCollectList.setLayoutManager(new GridLayoutManager(mContext, 2));
        collectAdapter = new CollectAdapter();
        mDataBinding.rvCollectList.setAdapter(collectAdapter);
        collectAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivCollectEdit://更多
                setNum();
                mDataBinding.tvCollectTitle.setText(getString(R.string.manage_collect));
                mDataBinding.llCollectBottom.setVisibility(View.VISIBLE);
                mDataBinding.ivMyCollectBack.setVisibility(View.GONE);
                mDataBinding.ivCollectEdit.setVisibility(View.GONE);
                mDataBinding.tvAllSel.setVisibility(View.VISIBLE);
                mDataBinding.tvCancelEdit.setVisibility(View.VISIBLE);
                isEdit = true;
                collectAdapter.setEdit(isEdit);
                collectAdapter.notifyDataSetChanged();
                break;
            case R.id.tvAllSel://全选
                listSel.clear();
                for (StkResBean bean : listShow) {
                    bean.setSelected(false);
                    listSel.add(bean);
                    bean.setSelected(true);
                }
                setNum();
                collectAdapter.notifyDataSetChanged();
                break;
            case R.id.tvCancelEdit://取消编辑
                listSel.clear();
                mDataBinding.tvCollectTitle.setText(getString(R.string.my_collect));
                mDataBinding.llCollectBottom.setVisibility(View.GONE);
                mDataBinding.ivMyCollectBack.setVisibility(View.VISIBLE);
                mDataBinding.ivCollectEdit.setVisibility(View.VISIBLE);
                mDataBinding.tvAllSel.setVisibility(View.GONE);
                mDataBinding.tvCancelEdit.setVisibility(View.GONE);
                isEdit = false;
                collectAdapter.setEdit(isEdit);
                getData();
                break;
            case R.id.tvCancelCollect://取消收藏
                if (listSel.size() == 0) return;
                List<StkResBean> listSp = AppSpUtils.getCollectList();
                listSp.removeAll(listSel);
                AppSpUtils.saveCollectList(listSp);
                listSel.clear();
                getData();
                setNum();
                break;
            case R.id.tvDownload://下载模板
                if (listSel.size() == 0) return;
                if (listSel.size() > 1) {
                    ToastUtils.showShort(getString(R.string.please_sel_one_template));
                    return;
                }
                showDialog(getString(R.string.save_ing));
                Downloader.newTask(mContext)
                        .url(listSel.get(0).getUrl())
                        .saveToPublic(true)
                        //Android10及以上，保存到外部公共存储空间需提供文件名称
                        .fileName(System.currentTimeMillis() + ".ppt")
                        .start(new Downloader.ICallback() {
                            @Override
                            public void onStart(int downloadTaskId) {

                            }

                            @Override
                            public void onProgress(long currentOffset, long totalLength, int percent) {

                            }

                            @Override
                            public void onComplete(@NonNull Uri uri) {
                                dismissDialog();
                                ToastUtils.showShort(getString(R.string.download_suc));
                            }

                            @Override
                            public void onFail(FailCause cause) {
                                dismissDialog();
                                ToastUtils.showShort(R.string.download_def);
                            }
                        });
                break;
            case R.id.ivGoHome://返回首页
                ActivityUtils.finishActivity(HomeActivity.class);
                startActivity(HomeActivity.class);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (collectAdapter.getItem(position).isSelected()) {
            collectAdapter.getItem(position).setSelected(false);
            listSel.remove(collectAdapter.getItem(position));
        } else {
            listSel.add(collectAdapter.getItem(position));
            collectAdapter.getItem(position).setSelected(true);
        }
        collectAdapter.notifyItemChanged(position);
        setNum();

    }

    @Override
    protected void initData() {
        getData();
        setNum();
    }

    private void getData() {
        listShow.clear();
        List<StkResBean> listSp = AppSpUtils.getCollectList();
        if (listSp != null && listSp.size() > 0) {
            listShow.addAll(listSp);
            collectAdapter.setList(listShow);
            mDataBinding.rvCollectList.setVisibility(View.VISIBLE);
            mDataBinding.llCollectNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvCollectList.setVisibility(View.GONE);
            mDataBinding.llCollectNoData.setVisibility(View.VISIBLE);
        }
    }

    private void setNum() {
        mDataBinding.tvAllSel.setText(getString(R.string.all_sel, listSel.size()));
        mDataBinding.tvCancelCollect.setText(getString(R.string.cancel_collect_num, listSel.size()));
        mDataBinding.tvDownload.setText(getString(R.string.download_template_num, listSel.size()));
    }

}